[-*Smarty*-]
[-*****************************************************************************-]
[-* Copyright (C) 2005-2006 Rod Roark <rod@sunsetsystems.com> and others      *-]
[-*                                                                           *-]
[-* This program is free software; you can redistribute it and/or             *-]
[-* modify it under the terms of the GNU General Public License               *-]
[-* as published by the Free Software Foundation; either version 2            *-]
[-* of the License, or (at your option) any later version.                    *-]
[-*****************************************************************************-]
[-config_load file="default.conf"-]
[-*Load the Language Definitions*-]
[-config_load file="lang.$USER_LANG"-]
[-include file="$TPL_NAME/views/header.html"-]
[-* we want to include out stylesheet for this view*-]
[-fetch file="$TPL_STYLE_PATH/day.css" assign="css"-]
[-eval var=$css-]
[-if $PRINT_VIEW != 1-]
[-*Main Navigation*-]
[-include file="$TPL_NAME/views/global/navigation.html"-]
[-/if-]

<script language='JavaScript'>
 Calendar.setup({inputField:"jumpdate", ifFormat:"%Y-%m-%d", button:"img_jumpdate"});
</script>

[-assign var="dayname" value=$DATE|date_format:"%w"-]
[-assign var="day"     value=$DATE|date_format:"%d"|string_format:"%1d"-]
[-assign var="month"   value=$DATE|date_format:"%m"|string_format:"%1d"-]
[-assign var="year"    value=$DATE|date_format:"%Y"|string_format:"%4d"-]

[-pc_sort_events var="S_EVENTS" sort="time" order="asc" value=$A_EVENTS-]

[-php-]

 echo "<table width='100%' border='1' cellpadding='1' cellspacing='0' >\n";
 $tdstylearr = array('bordyy', 'bordny', 'bordyn', 'bordnn');

 // For each day...
 foreach ($A_EVENTS as $date => $events) {
  $need_headers = true;
  $eventdate = substr($date, 0, 4) . substr($date, 5, 2) . substr($date, 8, 2);
  //(CHEMED) set MULTIDAY otherwise day header is not shown. This is what makes week view different from day view
  $MULTIDAY = true;
  // If multiple days then show a date header for each.
  if ($MULTIDAY) {
   echo " <tr>\n";
   //(CHEMED) added color
   echo "  <td colspan='" . (count($providers) * 2) . "' align='center' bgcolor='#B0C4DE'>" .
        dateformat(strtotime($date), true) . "</td>";
   echo " </tr>\n";
  }

  $arr_events = $S_EVENTS[$date];
  list($slotkey, $slotevent) = each($arr_events);

  // This is an array of provider status information for this day,
  // used to properly assign table cell attributes.
  $provstat = array();

  $slotindex = 0;
  $lastslotindex = count($times) - 1;

  // For each time slot...
  foreach ($times as $slottime) {
   $startampm = ($slottime['mer']) == "pm" ? 2 : 1;
   $starttimeh = $slottime['hour'];
   $starttimem = $slottime['minute'];
   $slotendmins = $starttimeh * 60 + $starttimem + $interval;

   // Repeat doc names at 1PM.  This is a kludge; omit it for released code.
   // if ($starttimeh == 13 && $starttimem == 0) $need_headers = true;

   // Get all events for all providers just for this time slot now, because we
   // can pick up where we left off and because we don't want to re-scan all
   // events for the day for each table cell.
   //
   $arr_slot = array();
   for (; isset($slotkey); list($slotkey, $slotevent) = each($arr_events)) {
    $starth = substr($slotevent['startTime'], 0, 2);
    $startm = substr($slotevent['startTime'], 3, 2);

    if (($starth * 60 + $startm) >= $slotendmins) break;

    $arr_slot[$slotkey] = $slotevent;

    $catid = $slotevent['catid'];
    $providerid = $slotevent['aid'];
    $durminutes = ceil($slotevent['duration'] / 60);
    $durslots = ceil($durminutes / $interval);

    // While we're here, collect information for cell cosmetics.
    if ($catid == 2) { // in office
     $provstat[$providerid]['in'] = true;

     // Save preferred category info for this IN event.
     $provstat[$providerid]['in_cat'] = -1;
     $provstat[$providerid]['in_slot0'] = $slotindex;
     $provstat[$providerid]['in_slots'] = 1;
     if ($slotevent['prefcatid']) {
      foreach ($A_CATEGORY as $catkey => $catval) {
       if ($catval['id'] == $slotevent['prefcatid']) {
        $provstat[$providerid]['in_cat'] = $catkey;
        $provstat[$providerid]['in_slots'] =
         ceil(ceil($catval['event_duration'] / 60) / $interval);
        break;
       }
      }
      // Debugging:
      if ($provstat[$providerid]['in_cat'] < 0) {
       echo "<!-- Unmatched prefcatid = " . $slotevent['prefcatid'] . " -->\n"; // debugging
      }
     }

    }
    else if ($catid == 3) { // out of office
     $provstat[$providerid]['in'] = false;
    }
    else if ($catid == 4 || $catid == 8 || $catid == 11) { // unavailable types
     // unavailable slots are marked 'res' for appropriate coloring.
     $endindex = $slotindex + $durslots;
     for ($i = $slotindex; $i < $endindex; ++$i) {
      $provstat[$providerid][$i]['res'] = true;
     }
    }
    // If duration > slot size then mark slots for border control.  A slot
    // marked 'ext' is not to have a border between itself and the slot below.
    if ($durslots > 1) {
     $endindex = $slotindex + $durslots - 1;
     for ($i = $slotindex; $i < $endindex; ++$i) {
      $provstat[$providerid][$i]['ext'] = true;
     }
    }
   }
   // Finished getting event information for this time slot.

   // Write a header row with the provider names if appropriate.
   if ($need_headers) {
    $need_headers = false;
    echo " <tr>\n";
    foreach ($providers as $provider) {
     echo "  <td colspan='2' align='center'>";
     echo $provider['fname'][0] . " " . $provider['lname'];
     echo "</td>\n";
    }
    echo " </tr>\n";
   }

   echo " <tr>\n";

   // We are now ready to write the table row for the current time slot.
   // This loops once for each provider to be displayed.
   //
   foreach ($providers as $provider) {
    $providerid = $provider['id'];

    // Mark this as a no-bottom-border slot if the preferred category
    // duration extends beyond it.
    //
    // echo "<!-- slotindex=$slotindex in=" . $provstat[$providerid]['in'] .
    //  " in_cat=" . $provstat[$providerid]['in_cat'] .
    //  " in_slot0=" . $provstat[$providerid]['in_slot0'] .
    //  " in_slots=" . $provstat[$providerid]['in_slots'] . " -->\n"; // debugging
    //
    if ($provstat[$providerid]['in']) {
     if (($slotindex + 1 - $provstat[$providerid]['in_slot0']) % $provstat[$providerid]['in_slots'])
      $provstat[$providerid][$slotindex]['ext'] = true;
    }

    $content = ""; // this will be the event cell content

    $clsuffix = "acell";
    if ($provstat[$providerid][$slotindex]['res'] || ! $provstat[$providerid]['in']) {
      $clsuffix = "ucell";
    }

    // Remove top and/or bottom table cell borders using CSS when events span
    // time slots or when the practitioner is not in-office.  Using "rowspan"
    // would be a difficult and inferior solution to this problem.
    //
    $tdstyleix = 0;
    $timelabel = true;
    if ($slotindex < $lastslotindex &&
        ($provstat[$providerid][$slotindex]['ext'] || !$provstat[$providerid]['in'])) {
     $tdstyleix += 1; // no bottom
    }
    if ($slotindex > 0 && ($provstat[$providerid][$slotindex-1]['ext'] ||
        !($provstat[$providerid]['in'] || $provstat[$providerid]['wasin']))) {
     $tdstyleix += 2; // no top
     $timelabel = false;
    }
    $tdstyle = ' ' . $tdstylearr[$tdstyleix];

    $disptimeh = ($starttimeh > 12) ? ($starttimeh - 12) : $starttimeh;

    $in_cat_id = 0;
    if ($provstat[$providerid]['in_cat'] >= 0) {
     $category =& $A_CATEGORY[$provstat[$providerid]['in_cat']];
     if ($category['id']) $in_cat_id = $category['id'];
    }

    // If a slot omits the top border, then we omit the TOD cell's top border
    // and skip writing its time-of-day text.  This way we can get some rows
    // with no content at all, which should produce a more compact and readable
    // calendar display.

    echo "  <td class='t$clsuffix$tdstyle'";
    if ($slotindex == 0) echo " width='1%'";
    echo ">";
// (CHEMED) always show timelabel    if ($timelabel) {
     echo "<a href='javascript:newEvt($startampm,$starttimeh,$starttimem,$eventdate,$providerid,$in_cat_id)'>";
     echo "$disptimeh:$starttimem</a>";
//    } else {
//     echo "<div class=tiny></div>"; // 1x1 px content so the browser does not suppress borders
// END (CHEMED)   }
    echo "</td>\n";

    echo "  <td class='e$clsuffix$tdstyle'";
    if ($clsuffix == "acell" && $in_cat_id) {
     echo " style='background-color:" . $category['color'] . "'";
     echo " title='" . $category['name'] . "'";
    }
    echo ">";

    // Scan all events for this time slot and generate the associated HTML for
    // this doc.  JavaScript is used in hrefs to reduce the volume of output.
    //
    reset($arr_slot);
    while (list($eventkey, $event) = each($arr_slot)) {
     if ($event['aid'] != $providerid) continue;
     if ($content) $content .= " ";
     $starth = substr($event['startTime'], 0, 2);
     $startm = substr($event['startTime'], 3, 2);
     $eventid = $event['eid'];
     $patientid = $event['pid'];
     $commapos = strpos($event['patient_name'], ",");
     $lname = addslashes(ucfirst(strtolower(substr($event['patient_name'], 0, $commapos))));
     $fname = addslashes(ucfirst(strtolower(substr($event['patient_name'], $commapos + 2))));
     $patient_dob = $event['patient_dob'];
     $patient_age = $event['patient_age'];
     $catid = $event['catid'];
     $comment = addslashes($event['hometext']);
     $catname = $event['catname'];
     $title = "Age $patient_age ($patient_dob)";
     if ($comment && $GLOBALS['calendar_appt_style'] < 4)
      $title .= " " . $comment;
     if ($catid == 2 || $catid == 3 || $catid == 4 || $catid == 8 || $catid == 11) {
      if      ($catid ==  2) $catname = "IN";
      else if ($catid ==  3) $catname = "OUT";
      else if ($catid ==  4) $catname = "VACATION";
      else if ($catid ==  8) $catname = "LUNCH";
      else if ($catid == 11) $catname = "RESERVED";
      // Omit lunch, vacation, etc. if the doc is not in-office.
            // Omit lunch, vacation, etc. if the doc is not in-office.
      if ($provstat[$providerid]['in'] || $catid < 4) {
       $content .= "<a href='javascript:oldEvt($eventdate,$eventid)'>";
//       $content .="<span style='background-color: ".$event[catcolor]."'>";   //(CHEMED) Color events by category start
       $content .= $catname;
       if ($comment) $content .= " - $comment";
//       $content .="</span>";                                                 //(CHEMED) Color events by category end
       $content .= "</a>";
      }
     }
     else { // some sort of patient appointment
         $content .= "<a href='javascript:oldEvt($eventdate,$eventid)' title='$catname'>";
      $content .="<span style='background-color: ".$event[catcolor]."' class='appttime'>&nbsp;";    //(CHEMED) Color events by category start
      $content .= $starth . ':' . $startm . "<span class='apptstatus'>".htmlspecialchars($event['apptstatus'])."</span>"; //(CHEMED) added 'span' to apptstatus
      $content .="</span>";                                                  //(CHEMED) Color events by category end
      $content .= "</a>";
      $content .= "<a href='javascript:goPid($patientid)' title='$title'>";
      $content .="<span style='background-color: ".$event[catcolor]."' class='apptname'>";    //(CHEMED) Color events by category start
      if ($catid == 1) $content .= "<strike>";
      $content .= $lname;
      if ($GLOBALS['calendar_appt_style'] != 1) {
       $content .= "," . $fname;
       if ($event['title'] && $GLOBALS['calendar_appt_style'] >= 3) {
        $content .= "(" . $event['title'];
        if ($event['hometext'] && $GLOBALS['calendar_appt_style'] >= 4)
         $content .= ": <font color='green'>" . htmlspecialchars(trim($event['hometext'])) . "</font>";
        $content .= ")";
       }
      }
      if ($catid == 1) $content .= "</strike>";
      $content .="&nbsp;</span>";                                                  //(CHEMED) Color events by category end
      $content .= "</a>";
     }
    } // end while

    if (! $content) $content = "<div class=tiny></div>"; // so the browser does not suppress borders
    echo $content . "</td>\n";

    // Keep track of whether the doc was in during the previous time slot.
    $provstat[$providerid]['wasin'] = $provstat[$providerid]['in'];

   } // end provider

   echo " </tr>\n";

   ++$slotindex;
  } // end time slot
 } // end day

 echo "</table>\n";

 // [-*footer*-]
 // [-include file="$TPL_NAME/views/global/footer.html"-]
 // [-include file="$TPL_NAME/views/footer.html"-]

[-/php-]

</body>
</html>
